package com.example.waimai.util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.waimaiandroid.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class RequestPermissionUtil {

    // 需要申请的权限列表
    private static  String[] permission = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET,
    };

    public static void requestPermission(Activity activity){
        // 当API大于 23 时，才动态申请权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 生成一个需要动态授权的权限列表
            List<String> permissionList = new ArrayList<>();
            for(String str : permission){
                if(ContextCompat.checkSelfPermission(activity,str) != PackageManager.PERMISSION_GRANTED){
                    // 如果没有被授权
                    // 就将其加入需要动态授权的列表中
                    permissionList.add(str);
                }
            }
            if(permissionList.size() >0){
                String[] needPermission = permissionList.toArray(new String[permissionList.size()]);
                ActivityCompat.requestPermissions(activity,needPermission,1);
            }

        }
    }
}
